<%@page import="java.text.SimpleDateFormat"%>
<%@page language="java" import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Detail</title>
        <link rel="stylesheet" type="text/css" title="T1" href="stylesheet/progin.css" id="link_theme" />
        <link rel="stylesheet" type="text/css" title="T1" href="stylesheet/detail.css" />
		<meta name="description" content="RER Gag, Make You Laughing" />
		<meta name="keywords" content="laugh" />
		<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
                <script type="text/javascript" src="script/home.js"></script>
    </head>
    <body>
        <%@ include file="header.jsp" %>
            
        <div class="main_container">
        <%
            String driver = "org.gjt.mm.mysql.Driver";
            Class.forName(driver).newInstance();

            Connection con=null;
            ResultSet rst=null;
            Statement stmt=null;
            Statement stmt_1 = null;
            ResultSet rst_1 = null;

            try{
                String url="jdbc:mysql://localhost:3306/progin_171_13509008?user=root";
                con=DriverManager.getConnection(url);
                stmt=con.createStatement();
                stmt_1=con.createStatement();
            }
            catch(Exception e){
                    System.out.println(e.getMessage());
            }
            
            // get session id_content which we want to display
            int id_temp = (Integer)(session.getAttribute("id_content"));
            String user_name = (String)(session.getAttribute("user_name"));
            
            try{ 
                rst = stmt.executeQuery("Select title,id_content as id_konten,content_type, content_filled, content_description, posting_time, user_name from content where id_content = "+id_temp+"");
            } catch (SQLException e){
                out.println("Caught SQL Exception: " + e);
            }
            
            while (rst.next()){
                if(rst.getString("content_type").equals("link")){
                    out.println("<h1><a href='detail.jsp'>"+rst.getString("title")+"</a></h1>");
                    out.println("<h2>"+rst.getString("content_filled")+"</h2>");
                    out.println("<h3>"+rst.getString("content_description")+"</h3>");
                    out.println("<h3>Posted by: <a href='profile.jsp'>"+rst.getString("user_name")+"</a></h3>");
                }
                if(rst.getString("content_type").equals("image")){
                    out.println("<h1><a href='detail.jsp'>"+rst.getString("title")+"</a></h1>");
                    out.println("<img src=\""+rst.getString("content_filled")+"\" height=500px; ></img>");
                    out.println("<h3>Posted by: <a href='profile.jsp'>"+rst.getString("user_name")+"</a></h3>");
                }
                if(rst.getString("content_type").equals("video")){
                    out.println("<h1><a href='detail.jsp'>"+rst.getString("title")+"</a></h1>");
                    out.println(rst.getString("content_filled"));
                    out.println("<h3>Posted by: <a href='detail.jsp'>"+rst.getString("user_name")+"</a></h3>");
                }

                out.println("<input id='like_"+rst.getString("id_konten")+"' class='like_button' type='image' src='images/like_button.jpg' onclick='like(\""+user_name+"\","+rst.getString("id_konten")+")'/>");
                out.println("<input class='total_like_class' type='text' disabled='disabled' id='total_like_"+rst.getString("id_konten")+"' />");
                out.println("<input id='dislike_'"+rst.getString("id_konten")+" class='like_button' type='image' src='images/dislike_button.jpg' onclick='dislike(\""+user_name+"\","+rst.getString("id_konten")+")'/>");
                out.println("<input class='total_dislike_class' type='text' disabled='disabled' id='total_dislike_"+rst.getString("id_konten")+"' />");

                out.println("<script type='text/javascript'>"); //hitung nilai like dan dislike dulu!
                out.println( "likeUpdater("+rst.getString("id_konten")+"); dislikeUpdater("+rst.getString("id_konten")+");");
                out.println( "</script>");
            }
            // Get current datetime
            java.util.Date now = new java.util.Date();
            String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
            String strDateNew = sdf.format(now) ;
            
            
            // show time Posting
            String queryGetTime = "SELECT CONCAT(FLOOR(HOUR(TIMEDIFF(posting_time, '"+strDateNew+"')) / 24), ' days ',MOD(HOUR(TIMEDIFF(posting_time, '"+strDateNew+"')), 24), ' hours ',MINUTE(TIMEDIFF(posting_time, '"+strDateNew+"')), ' minutes') as time_difference from content where id_content="+id_temp+"";
            try{ 
                rst_1 = stmt_1.executeQuery(queryGetTime);
            } catch (SQLException e){
                out.println("Caught SQL Exception: " + e);
            }
            String timeDiff ="";
            while (rst_1.next()){
                timeDiff = rst_1.getString("time_difference");
            }
            out.println("<br/>Posting Time:  "+timeDiff+" ago<br/>");
        
       
            // get Session
            //String id_temp = (String)(session.getAttribute("id_content"));
            String userFromSession = (String)(session.getAttribute("user_name"));
        
            
            // get next id_content for image naming
            
            int curIDComment =0;
            String qCount = "SELECT MAX(id_comment) FROM comment";

            try{ 
                rst = stmt.executeQuery(qCount);
            } catch (SQLException e){
                out.println("Caught SQL Exception: " + e);
            }
            while (rst.next()){
                String temp = rst.getString("MAX(id_comment)");
                if (temp != null){
                    curIDComment = Integer.parseInt(temp);
                }
            }
            curIDComment++;
            out.println("<div class='rect'><div class='comment'><form id='comment_form' name='comment_form' method='post' enctype='multipart/form-data'><textarea id='comment_form_textarea' name='text_comment' cols='30' rows='5'></textarea><input type='button' id='comment_form_submit' value='Comment' onclick='submit_comment(\""+userFromSession+"\","+curIDComment+")'/></form><div id='commentList'>");
            
            // id Content nanti diubah pake session dari user
            String queryGetTotalComment = "select count(id_comment) as totalComment  from comment where id_content="+id_temp+"";
            try{ 
                rst = stmt.executeQuery(queryGetTotalComment);
            } catch (SQLException e){
                out.println("Caught SQL Exception: " + e);
            }
            
            while(rst.next()){
                out.println("<h3 id=\"totalComment\">Total Comment : "+rst.getString("totalComment")+"</h3>");
            }
            
            // query sort comment by time
            String querySelectComment = "select id_comment,user_name, comment_text, Concat ( Floor(Hour(TIMEDIFF (comment_time, '"+strDateNew+"'))/24), ' days ',MOD(HOUR(TIMEDIFF(comment_time, '"+strDateNew+"')), 24), ' hours ', MINUTE (TIMEDIFF(comment_time, '"+strDateNew+"')), ' minutes')as time from comment natural join user_comment where comment.id_content ="+id_temp+" order by time";

            try{ 
                rst = stmt.executeQuery(querySelectComment);
            } catch (SQLException e){
                out.println("Caught SQL Exception: " + e);
            }        
            //$resultGetComment = mysql_query($querySelectComment);

            while (rst.next()){
                out.println("<div class='comment_list'>");
                out.println("<img src='images/logo.png' width='50px' height='50px' style='float: left;'></img>");
                out.println("<button type='button' onclick='deleteComment(\""+userFromSession+"\","+rst.getString("id_comment")+")' style='float: right;'>X</button>");
                out.println("<h5>"+rst.getString("user_name")+"</h5><br/>");
                out.println("<h5>"+rst.getString("time")+"</h5><br/>");
                out.println("<h3>"+rst.getString("comment_text")+"</h3>");
                out.println("</div>");
            }
            out.println("<script type='text/javascript' src='script/detail.js'></script></div></div></div>");
            
        %>
       
        <script type="text/javascript" src="script/detail.js"></script>
        <script type="text/javascript" src="script/progin.js"></script>
        </div>
    </body>
</html>
